package com.feeyo.groundservice.vehiclemonitorserver;

import com.feeyo.groundservice.base.rdb.PostgresDb;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by fooevr on 2016/12/29.
 */
public class VehicleType extends AbstractVerticle {
	Logger logger = LoggerFactory.getLogger(VehicleType.class);
    public static Map<String, VehicleTypeObject> vehicleTypes = new HashMap<>();
    @Override
    public void start(Future<Void> startFuture) throws Exception {
    	try{
	        PostgresDb.postgreSQLClient.getConnection(conn->{
	            if(conn.failed()){
	                System.out.println(conn.cause());
	                return;
	            }
	            if(conn.succeeded()){
		            conn.result().query("select id, name, code from common.vehicletype", r->{
		                conn.result().close();
		                if(r.failed()){
		                    System.out.println(r.cause());
		                    return;
		                }
		                vehicleTypes.clear();
		                for(int i = 0; i < r.result().getNumRows(); i++){
		                    JsonObject row = r.result().getRows().get(i);
		                    VehicleTypeObject vehType = new VehicleTypeObject();
		                    vehType.id = row.getString("id");
		                    vehType.name = row.getString("name");
		                    vehType.code = row.getString("code");
		                    vehicleTypes.put(vehType.id, vehType);
		                }
		                startFuture.complete();
		                logger.info("load vehicle types " + vehicleTypes.size());
		            });
	            }
	        });
    	}catch (Exception e){
    		e.printStackTrace();
    	}
    }

    public class VehicleTypeObject{
        public String id;
        public String name;
        public String code;
    }
}
